<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="util.*"%>
<%@page import="com.google.gdata.data.contacts.ContactEntry, com.google.gdata.data.extensions.*"%>
<%@ page import="java.util.*"%>

<%
	List<ContactEntry> contactList = ContactUtil.retrieveAllContacts("buzzsystememail@gmail.com", "buzzsystemtest");

%>

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Contact List</title>
	
	<link rel="stylesheet" href="css/common.css" />
	<link rel="stylesheet" href="css/inputForm.css" />
	<link rel="stylesheet" href="css/listForm.css" />
	<link rel="stylesheet" href="css/jquery-ui-1.8.14.custom.css" type="text/css" />
	<link rel="stylesheet" href="css/hyjack.css" type="text/css" />
	<link rel="stylesheet" href="css/datepicker.css" type="text/css" />
	<link rel="stylesheet" href="css/themes/ui-lightness/jquery.ui.all.css" />
	<link rel="stylesheet" href="css/jquery-ui-timepicker.css" type="text/css" />
	<link rel="stylesheet" href="css/jquery-datepicker.css" />
	<link rel="stylesheet" href="css/jquery-dialog.css" />
	
	<script language="javascript" type="text/javascript" src="js/jquery.min.js"></script>
	<script language="javascript" type="text/javascript" src="js/jquery.hyjack.select.js"></script>
	<script language="javascript" type="text/javascript" src="js/jquery.ui.core.min.js"></script>
	<script language="javascript" type="text/javascript" src="js/jquery.ui.widget.min.js"></script>
	<script language="javascript" type="text/javascript" src="js/jquery.ui.timepicker.js"></script>
	<script language="javascript" type="text/javascript" src="js/jquery.ui.datepicker.js"></script>
	<script language="javascript" type="text/javascript" src="js/datepicker.js"></script>
	<script language="javascript" type="text/javascript" src="js/jquery.hyjack.js"></script>
	<script language="javascript" type="text/javascript" src="js/ajaxQuickInsert.js"></script>
	<script language="javascript" type="text/javascript" src="js/jquery.numeric.js"></script>
	<script language="javascript" type="text/javascript" src="js/jquery.ui.tools.min.js"></script>
	<script language="javascript" type="text/javascript" src="js/common.js"></script>
	<script type="text/javascript" src="js/jquery-ui-1.8.14.custom.min.js"></script>
	<script type="text/javascript" src="js/jquery-ui-timepicker-addon.js"></script>
	
	<script language="javascript" type="text/javascript">
		$(document).ready(pageLoaded);
		function pageLoaded()
		{	
			initDeleteConfirmation();
		} 
	</script>
</head>
<body>
	<div class="divContainer">	
	<br />
		<br />
		<br />
		<br />
		<a style="font-weight: bold;" href="contactList.jsp">Add new contact</a>
		<br />
		<br />
	<div class="divListHeaderBar">NUMBER OF CONTACTS: <%=contactList.size() %></div>
	<div class="divOverFlow">
	<table class="tblList">
		<tr>
			<th class="thListHeader functionColumnNormal" rowspan="2">Contact</th>
			<th class="thListHeader FullName" rowspan="2">Full name</th>
			<th class="thListHeader TextArea" rowspan="2">Address</th>
			<th class="thListHeader"  colspan="2">Email</th>
			<th class="thListHeader" colspan="4">Phone</th>
		</tr>
		<tr>
			<th class="thListHeader EmailAddress">Home</th>
			<th class="thListHeader EmailAddress">Work</th>

			<th class="thListHeader PhoneNumber">Mobile</th>
			<th class="thListHeader PhoneNumber">Home</th>
			<th class="thListHeader PhoneNumber">Work</th>
			<th class="thListHeader PhoneNumber">Other</th>
		</tr>

		<%
			int size = 0;
			if (contactList != null)
				size = contactList.size();
			
			for (int i = 0; i < size; i++) {
				ContactEntry contact = contactList.get(i);

				String strContactId = "";
				String strId = contact.getId();
				strContactId = strId.substring(strId.lastIndexOf('/') + 1);

				String strDisplayName = "";
				if (contact.hasName() && contact.getName().hasFullName())
				{
					FullName fullName = contact.getName().getFullName();
					strDisplayName = fullName.getValue();
					strDisplayName += (fullName.hasYomi() == true) ? fullName.getYomi() : ""; 
				}
				
				String strAddress = "";
				
				if (contact.hasStructuredPostalAddresses())
				{
					for (StructuredPostalAddress address : contact.getStructuredPostalAddresses())
					{
						strAddress += address.getFormattedAddress().getValue() + "\n";
					}
				}
				
				
				String[] strEmails = {"", "", ""}; //Home, Work, Other
				if (contact.hasEmailAddresses())
				{
					List<Email> emails = contact.getEmailAddresses();
					for (int k = 0; k < emails.size() && k < strEmails.length; k++)
					{
						strEmails[k] = emails.get(k).getAddress();
					}
				}
				
				String[] strPhoneNumbers = {"", "", "", ""};//Mobile, Home, Work, Other
				if (contact.hasPhoneNumbers())
				{
					List<PhoneNumber> phones = contact.getPhoneNumbers();
					for (int j = 0; j < phones.size() && j < strPhoneNumbers.length; j++)//PhoneNumber phoneNo : contact.getPhoneNumbers())
					{
						strPhoneNumbers[j] = phones.get(j).getPhoneNumber() + "\n";
					}
				}
				
				
		%>

		<tr class="trListItem">
			<td class="tdListText">
				<span><%=i + 1%></span>
				<br/>
				<span class="spanEdit"><a href="contact?mode=edit&id=<%=strContactId%>">Edit</a></span>
				<br/>
				<span class="spanEdit"><a class="hrefDelete" href="contact?mode=delete&id=<%=strContactId%>"  onclick="confirmToDelete();">Delete</a></span>
			</td>
			
			<td class="tdListText Fullname"><%=strDisplayName%></td>
			<td class="tdListTextarea"><%=strAddress%></td>
			<td class="tdListText EmailAddress"><%=strEmails[0] %></td>
			<td class="tdListText TextArea"><%=strEmails[1] %></td>
<%-- 			<td class="tdListText EmailAddress"><%=strEmails[2] %></td> --%>
			<td class="tdListText PhoneNumber"><%=strPhoneNumbers[0] %></td>
			<td class="tdListText PhoneNumber"><%=strPhoneNumbers[1] %></td>
			<td class="tdListText PhoneNumber"><%=strPhoneNumbers[2] %></td>
			<td class="tdListText PhoneNumber"><%=strPhoneNumbers[3] %></td>
		</tr>
		<%
			}
		%>
	</table>
	</div>
	<br />
	</div>
	
</body>
</html>